package cn.k.service.impl;

import cn.k.domain.Employee;
import cn.k.domain.Notice;
import cn.k.mapper.EmployeeMapper;
import cn.k.mapper.NoticeMapper;
import cn.k.qo.NoticeQueryObject;
import cn.k.service.INoticeService;
import cn.k.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void insert(Notice notice) {
        //新增时,为当前登录管理员用户新增
        Employee employee = UserContext.getEmployee();
        notice.setName(employee.getName());
        //设置创建时间
        notice.setCreateTime(new Date());

        //先更新notice表中的id
        noticeMapper.insert(notice);

        //管理员新增公告, 关系也要更新,批量添加,查看所有员工的id,加进关系
        List<Employee> employees = employeeMapper.selectAll();
        for (Employee employee1 : employees) {
            Long employee1Id = employee1.getId();
            noticeMapper.insertRelation(notice.getId(),employee1Id,notice.getReadId());
        }

    }

    @Override
    public void delete(Long id) {
        //删除公告,需要删除关系
        //删除信息根据当前用户删除对应关系
        Employee employee = UserContext.getEmployee();
        noticeMapper.deleteRelation(id,employee.getId());
        //noticeMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void update(Notice notice) {

        //员工已读,更新公告中间表的状态
        Employee employee = UserContext.getEmployee(); //获得当前登录的用户
        if (!employee.isAdmin()){
            noticeMapper.updateByPrimaryKey(notice);
            noticeMapper.updateReadRelation(notice.getId(),employee.getId(),notice.getReadId());
        }

    }

    @Override
    public Notice get(Long id) {
        Notice notice = noticeMapper.selectByPrimaryKey(id);
        return notice;
    }

    @Override
    public List<Notice> selectAll() {
        List<Notice> notices = noticeMapper.selectAll();
        return notices;
    }


    @Override
    public List<Notice> selectForList(NoticeQueryObject qo,int releaseStatus) {
        List<Notice> notices = noticeMapper.selectForList(qo,-1);
        return notices;
    }

    @Override
    public PageInfo<Notice> query(NoticeQueryObject qo) {
        //告诉插件我们查询第几页,每页多少条
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"n.create_time desc");

         //判断是否管理员用户名
        Employee employee = UserContext.getEmployee();
        //加入用户id
        qo.setEmployeeId(employee.getId());
        if (employee.isAdmin()){
            List<Notice> notices = noticeMapper.selectForList(qo,-1);
            return new PageInfo<>(notices);
        }else {
            List<Notice> notices = noticeMapper.selectForList(qo, 1);
            return new PageInfo<>(notices);

        }


        //查询当前页数据
        //List<Notice> notices = noticeMapper.selectForList(qo, false);

    }

    @Override
    public void updateReleaseStatus(Long id, int releaseStatus) {
        noticeMapper.updateReleaseStatus(id,releaseStatus);
    }

    @Override
    public Notice queryByReadRelation(Long id, Long employeeId) {
        return noticeMapper.selectByReadRelation(id,employeeId);
    }

    @Override
    public void updateReadRelation(Long id, Long employeeId, int status) {
        noticeMapper.updateReadRelation2(id,employeeId,status);
    }
}
